package com.yihuier.yihuier.Controller.MeetingManager;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.fasterxml.jackson.databind.util.JSONWrappedObject;
import com.yihuier.yihuier.Controller.Common.FileController;
import com.yihuier.yihuier.Entity.RoomManager.Room;
import com.yihuier.yihuier.Service.Common.FileUploadService;
import com.yihuier.yihuier.Service.MeetingManager.MeetingRoomService;
import com.yihuier.yihuier.Service.MeetingManager.MeetingService;
import com.yihuier.yihuier.Service.MeetingManager.MeetingUserService;
import com.yihuier.yihuier.Service.RoomManager.RoomService;
import com.yihuier.yihuier.Service.UserManager.DepartmentService;
import com.yihuier.yihuier.Service.UserManager.DepartmentUserService;
import com.yihuier.yihuier.Service.UserManager.UserService;
import com.yihuier.yihuier.Util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.nio.file.Path;
import java.util.*;

@RestController
@RequestMapping("/MeetingManager/MeetingAndRooms/")
public class MeetingRoomController {

    @Autowired
    MeetingRoomService meetingRoomService;

    @Autowired
    RoomService roomService;

    @Autowired
    MeetingService meetingService;

    @Autowired
    MeetingUserService meetingUserService;

    @Autowired
    UserService userService;

    @Autowired
    DepartmentUserService departmentUserService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    FileUploadService fileUploadService;

    @Value("${yihuier.QrCode.disposit}")
    private Path QrCode_Path;


    @GetMapping("topThreeDepartmentRatio")
    public ResponseMessage getTopThreeDepartmentRatio(@PathParam("room_id") int room_id, @PathParam("section") int section){
        if (room_id <= 0){
            return new ResponseMessage(1,"请求失败：无法找到会议室ID为0的会议室");
        }
        Date after = new Date();
        Date before;
        switch (section){
            case 7:
                before =  new Date(after.getTime() - 604800000L);
                break;
            case 30:
                before =  new Date(after.getTime() - 2592000000L);
                break;
            case 365:
                before =  new Date(after.getTime() - 31536000000L);
                break;
            default:
                before = new Date(after.getTime() - 604800000L);
                break;
        }

        int total = meetingRoomService.getNumMeetings(before,after);

        List<Map<String,Object>> result = meetingRoomService.getTopThreeDepartmentRatio(before,after,room_id);

        for (Map<String,Object> nodeData : result){
            nodeData.put("ratio",(long)(nodeData.get("meeting_num"))*1.0/total);
            nodeData.remove("department_id");
            nodeData.remove("meeting_num");
        }

        return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功",result);
    }

    @PostMapping("searchDetail.json")
    public ResponseMessage getSearchDetail(@PathParam("id")int id,@PathParam("type")String type){
        if (type.equals("1")){//会议室信息
            Room room = null;
            try{
                room = roomService.getRoomById(id);
            }catch (Exception e){
                e.printStackTrace();
            }
            if (room == null){
                return new ResponseMessage(ResponseMessage.ERROR,"空指针异常");
            }
            LinkedHashMap<String,Object> roomInfomation = new LinkedHashMap<String,Object>();
            roomInfomation.put("room_name",room.getRoom_name());
            roomInfomation.put("room_capacity",room.getRoom_capacity());
            roomInfomation.put("room_parentstatus",room.getRoom_parentstatus());
            roomInfomation.put("room_status",room.getRoom_status());
            roomInfomation.put("room_pic_url", FileController.filePathDispose(room.getRoom_pic_url()));
            String[] hardware = room.getRoom_hardware().split(",");
            String QrCode = "http://sylardaemon.tech/file/meeting_room_QrCode/11.jpg";//默认二维码图片
            if (fileUploadService.findFileIsExist(QrCode_Path,"1"+id+".jpg")){
                QrCode = "http://sylardaemon.tech/file/meeting_room_QrCode/1"+id+".jpg";
            }
            roomInfomation.put("QrCode",QrCode);
            roomInfomation.put("room_hardware",hardware);
            return new ResponseMessage(ResponseMessage.SUCCESS,"会议室信息获取成功",roomInfomation);
        }else if(type.equals("2")){//会议信息
            Map<String,Object> meeting = meetingService.getMeetingInfoByMeetingId(id);
            String room_name = roomService.getRoomNameByRoomId((Long) meeting.get("location"));
            meeting.put("location",room_name);
            meeting.put("description",meeting.get("description"));
            String QrCode = "http://sylardaemon.tech/file/meeting_room_QrCode/01.jpg";//默认二维码图片
            if (fileUploadService.findFileIsExist(QrCode_Path,"0"+id+".jpg")){
                QrCode = "http://sylardaemon.tech/file/meeting_room_QrCode/0"+id+".jpg";
            }
            meeting.put("QrCode",QrCode);
            int[] attendUsersIds = meetingUserService.gettAttendUsersIds((Long)meeting.get("meeting_id"));
            List<Map<String,Object>> userInfo = userService.getUsersByUserIds(attendUsersIds);
            for (int i=0;i<userInfo.size();i++) {
                userInfo.get(i).put("user_headpic_dir",FileController.filePathDispose((String)userInfo.get(i).get("user_headpic_dir")));
            }
            meeting.put("users",userInfo);
            return new ResponseMessage(ResponseMessage.SUCCESS,"会议信息获取成功",meeting);
        }
        return new ResponseMessage(ResponseMessage.ERROR,"数据错误");
    }

    /**
     * 会议预定时显示会议室列表
     * */
    @PostMapping("getAllMeetingRoom.json")
    public ResponseMessage getRoomsByUserId(@PathParam("user_id") int user_id){
        int department_id = departmentUserService.getDepartmentIdByUserId(user_id);
        int company_id = departmentService.getCompanyIdByDepartmentId(department_id);
        return new ResponseMessage(ResponseMessage.SUCCESS,"请求成功",roomService.getRoomsByCompanyIdForOrder(company_id));
    }


}
